package com.hb0730.boot.admin.modules.sci.controller;

import com.hb0730.boot.admin.base.R;
import com.hb0730.boot.admin.data.domain.BasePage;
import com.hb0730.boot.admin.modules.sci.model.entity.SciPaperJournal;
import com.hb0730.boot.admin.modules.sci.model.query.SciPaperJournalQuery;
import com.hb0730.boot.admin.modules.sci.service.SciPaperJournalService;
import com.hb0730.boot.admin.security.model.UserInfo;
import com.hb0730.boot.admin.security.service.UserDetailServiceImpl;
import com.hb0730.boot.admin.security.token.RedisCacheUserProvider;
import com.hb0730.boot.admin.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sci/paper/journal")
@Tag(name = "科研：论文管理：期刊论文")
@RequiredArgsConstructor
public class SciPaperJournalController {
    private final SciPaperJournalService sciPaperJournalService;
    private final RedisCacheUserProvider redisCacheUserProvider;
    private final UserDetailServiceImpl userDetailService;


    @GetMapping("/query/page")
    @Operation(summary = "分页查询")
    public R<BasePage<SciPaperJournal>> queryPage(HttpServletRequest request, @ParameterObject SciPaperJournalQuery query) {
        BasePage<SciPaperJournal> res = this.sciPaperJournalService.queryPage(query);
        return R.OK(res);
    }

    @PostMapping("/save")
    @Operation(summary = "保存")
    public R<SciPaperJournal> save(HttpServletRequest request, @Validated @RequestBody SciPaperJournal sciPaperJournal) {
        UserInfo userInfo = SecurityUtil.getCurrentUser();
        sciPaperJournal.setDepartment(userInfo.getDepartment());
        sciPaperJournal.setUsername(userInfo.getUsername());
        sciPaperJournal.setNickname(userInfo.getNickname());
        sciPaperJournal.setStatus(1);
//        System.out.println(sciPaperJournal);
        return this.sciPaperJournalService.saveJournal(sciPaperJournal);
    }

    @PutMapping("/update/{id}")
    @Operation(summary = "修改")
    public R<SciPaperJournal> update(HttpServletRequest request, @PathVariable String id,
                                        @Validated @RequestBody SciPaperJournal sciPaperJournal) {
        return this.sciPaperJournalService.updateById(id, sciPaperJournal);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    public R<String> delete(HttpServletRequest request, @Validated @RequestBody List<String> ids) {
        if (null == ids || ids.isEmpty()) {
            return R.NG("删除失败，缺少参数");
        }
        return this.sciPaperJournalService.deleteByIds(ids);
    }
}
